package com.bjesc_b2c.app.appraiset.mvp.model.impl;

import android.content.Context;
import android.widget.Toast;

import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.mvp.model.DemandCarModel;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;
import java.util.List;

import master_bean.DemandCarBean;
import master_bean.FindCarBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/5/24.
 */

public class DemandCarModelImpl implements DemandCarModel {
    private CallBack callBack;
    public DemandCarModelImpl(CallBack callBack){
        this.callBack = callBack;
    }
    @Override
    public void getDemandCars(Context context, FindCarBean findBean) {
        HashMap<String, Object> build = new MapUtil()
                .put("page", findBean.getPage())
                .put("brand_id", findBean.getBrand_id())
                .put("price", findBean.getPrice())
                .put("installment", findBean.getInstallment())
                .put("type", findBean.getType())
                .put("keyword", findBean.getKeyword())
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).getDemandCars(),
                new BaseSubscriber<TokenBean<List<DemandCarBean>>>(context) {
                    @Override
                    public void onSuccess(TokenBean<List<DemandCarBean>> o) {
                        if(callBack!=null){
                            callBack.callback(o.getData());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                    }
                });
    }

    @Override
    public void addDemandCar(final Context context, String dispatchId, String carId) {
        HashMap<String, Object> build = new MapUtil()
                .put("dispatch_id", dispatchId)
                .put("car_id",carId)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).addDemandCar(),
                new BaseSubscriber<TokenBean>(context) {
                    @Override
                    public void onSuccess(TokenBean o) {
                        Toast.makeText(context,"添加成功",Toast.LENGTH_SHORT).show();
                        if(callBack!=null){
                            callBack.addSuccess();
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                    }
                });
    }

    public interface CallBack{
        void callback(List<DemandCarBean> listbean);
        void addSuccess();
    }
}
